<?php
/**
 * @file pushmessage.php $
 * @author LiangJian (liangjian@haibao.com) $
 * @date 2015年9月1日 下午12:44:08 $
 * @brief 推送消息 $
 */
namespace haibao\app\business;

use haibao\frame\FrameException;
class PushMessage extends \haibao\app\business\Base{
    
    /**
     * @var $_instance
     */
    private static $_instance;
    
    /**
     * instance()
     */
    private static function instance(){
        if(!self::$_instance){
            self::$_instance = new \haibao\app\data\PushMessage();
        }
        return self::$_instance;
    }
    
    public static function getPushMessageById($id){
        return self::instance()->getPushMessageById($id);
    }
    
    public static function getPushByQuery($query){
        return self::instance()->getAll($query);
    }
    
    public static function savePushMessage($model){
        self::checkOperation(self::FUNC_CODE_APP_LOGN_MESSAGE_ADD);
        $model->Id ? self::updatePushMessage($model) : self::addPushMessage($model);
        self::saveOperateHistory(self::FUNC_CODE_APP_LOGN_MESSAGE_ADD, $model->toArray(true,true));
    }
    
    /**
     * 删除推送消息记录
     */
    public static function deletePushMessage($id){
        self::checkOperation(self::FUNC_CODE_APP_LOGN_MESSAGE_ADD);
        self::instance()->updateStatusById($id, \haibao\app\model\data\PushMessage::STATUS_DISABLE);
        self::saveOperateHistory(self::FUNC_CODE_APP_LOGN_MESSAGE_ADD, $id);
    }
    
    /**
     * 根据推送记录ID修改发布状态
     */
    public static function updatePublishedById($id, $published = \haibao\app\model\data\PushMessage::PUBLISHED_YES){
        self::instance()->updatePublishedById($id, $published);
    }
    
    /**
     * @param \haibao\app\model\data\PushMessage $model
     */
    private static function checkForm($model){
        if(!isset($model->PushObject)){
            throw new BusinessException('发送对象不能为空');
        }
        if($model->PushObject != \haibao\app\model\data\PushMessage::PUSH_OBJECT_IOS && !$model->Title){
            throw new BusinessException('标题不能为空');
        }
        if(!$model->Content){
            throw new BusinessException('消息内容不能为空');
        }
        if(!isset($model->ArticleId)){
            throw new BusinessException('关联内容不能为空');
        }
        if($model->UserRange && $model->PushObject == \haibao\app\model\data\PushMessage::PUSH_OBJECT_ALL){
            throw new BusinessException('发送单个设备不能选择全部');
        }
    }
    
    /**
     * @param \haibao\app\model\data\PushMessage $model
     */
    private static function addPushMessage($model){
        self::checkForm($model);
        $model->ArticleId = (int)$model->ArticleId;
        if($model->PublishTime > date('Y-m-d H:i:s')){
            $model->Published = \haibao\app\model\data\PushMessage::PUBLISHED_PRE;
        }else{
            $model->Published = \haibao\app\model\data\PushMessage::PUBLISHED_ING;
        }
        $model->CreateBy = self::getCurrentUser()->Id;
        $model->Author = self::getCurrentUser()->RealName;
        $model->CreateTime = new \DateTime();
        $model->Status = \haibao\app\model\data\PushMessage::STATUS_ENABLE;
        self::instance()->addPushMessage($model);
    }
    
    /**
     * @param \haibao\app\model\data\PushMessage $model
     */
    private static function updatePushMessage($model){
        $pushMessage = self::getPushMessageById($model->Id);
        if($pushMessage->Published == \haibao\app\model\data\PushMessage::PUBLISHED_YES){
            throw new BusinessException('推送消息已经被发送成功，不能再继续修改！');
        }
        self::checkForm($model);
        $model->ArticleId = (int)$model->ArticleId;
        if($model->PublishTime > date('Y-m-d H:i:s')){
            $model->Published = \haibao\app\model\data\PushMessage::PUBLISHED_PRE;
        }else{
            $model->Published = \haibao\app\model\data\PushMessage::PUBLISHED_ING;
        }
        self::instance()->updatePushMessage($model);
    }
    
}